package com.wpam.scrabble.mobile.activities;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.wpam.scrabble.mobile.GameLogic;
import com.wpam.scrabble.mobile.R;
import com.wpam.scrabble.mobile.communication.CommunicationManager;
import com.wpam.scrabble.mobile.messages.Message;
import com.wpam.scrabble.mobile.messages.MsgSerializer;
import com.wpam.scrabble.mobile.messages.StartMessage;

public class WaitingForAcceptActivity extends Activity {

	private boolean ready_ = false;
	
	private TextView serverTypeTv_;
	private TextView turnTimeTv_;
	private TextView wordCheckTv_;
	private ImageView readyImage_;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_waiting_for_accept);
		
		serverTypeTv_ = (TextView) findViewById(R.id.serverTypeText);
		turnTimeTv_ = (TextView) findViewById(R.id.turnTimeText);
		wordCheckTv_ = (TextView) findViewById(R.id.wordCheckText);
		readyImage_ = (ImageView) findViewById(R.id.readynessImage);

		Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
		editor.putString(GameSettingsActivity.SERVER_TYPE, getIntent().getStringExtra(GameSettingsActivity.SERVER_TYPE));
		editor.putString(GameSettingsActivity.TURN_TIME, String.valueOf(getIntent().getIntExtra(GameSettingsActivity.TURN_TIME, 0)));
		editor.putBoolean(GameSettingsActivity.WORD_CHECK, getIntent().getBooleanExtra(GameSettingsActivity.WORD_CHECK, false));
		editor.commit();
		
		serverTypeTv_.setText(getIntent().getStringExtra(GameSettingsActivity.SERVER_TYPE));
		turnTimeTv_.setText(Integer.toString(getIntent().getIntExtra(GameSettingsActivity.TURN_TIME, 0)));
		wordCheckTv_.setText((getIntent().getBooleanExtra(GameSettingsActivity.WORD_CHECK, false)) ? "tak" : "nie");

		CommunicationManager.getInstance(this);
		
		readyImage_.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if (!ready_) {
					Builder builder = new Builder(v.getContext());
					builder.setTitle(getResources().getString(R.string.readyness));
					builder.setMessage(getResources().getString(R.string.are_you_ready_to_play));
					builder.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							readyImage_.setImageResource(R.drawable.ready);
							ready_ = true;
							dialog.dismiss();
							try {
								CommunicationManager.getInstance().writeAsClient(MsgSerializer.serialize(new Message(Message.READY)));
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
					builder.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
					builder.create().show();
				}
			}
		});
	}

	public void startGame(StartMessage startMessage) {
		
		GameLogic.getInstance().setStartMessage(startMessage);
		Intent gameBoardIntent = new Intent(this, BoardActivity.class);
		startActivity(gameBoardIntent);
		finish();
	}

	@Override
	public void onBackPressed() {

		CommunicationManager.disconnectClient();
		super.onBackPressed();
	}

	@Override
	protected void onResume() {

		CommunicationManager.getInstance(this);
		super.onResume();
	}
}